package com.example.handler;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.util.ThreadLocalUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyBatisPluHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        int id;
        if (ThreadLocalUtil.getCurrentId() != null) {
            // 处理非空情况
            long currentId = ThreadLocalUtil.getCurrentId();
            id = (int)currentId;
            // 后续业务逻辑
        } else {
            // 处理空值情况
           id = 1;
        }
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "createBy", Integer.class,id);
        this.strictUpdateFill(metaObject, "updateBy", Integer.class,id);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        int id;
        if (ThreadLocalUtil.getCurrentId() != null) {
            // 处理非空情况
            long currentId = ThreadLocalUtil.getCurrentId();
            id = (int)currentId;
            // 后续业务逻辑
        } else {
            // 处理空值情况
            id = 1;
        }
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updateBy", Integer.class,id);
    }
}